<?php
	class UserAction extends BaseAction{
		//显示注册页面
		public function register(){
			$face = array();//头像图片名
			for($i = 1; $i < 10; $i++)
				$face[$i] = '0'.$i;
			for($i = 10; $i < 25; $i++)
				$face[$i] = $i;
			$this->assign('face',$face);
			$this->display();
		}
		
		//完成注册功能
		public function reg(){
			if($_SESSION['verify'] != md5($_POST['verify'])) {
				$this->error('验证码错误！');
				exit();
			}
			$user = D('User');
			if($data = $user->create()){
				if($user->add()){
					//设置cookie保存登录状态
					$auth = md5($data['user'].'zgcms');
					cookie('name',$data['user'],0);
					cookie('auth',$auth,0);
					$this->success('注册账号成功！','__APP__');
				}else{
					$this->error('注册账号失败！');
				}
			}else{
				$this->error($user->getError());
			}
		}
		
		//显示登录页面
		public function login(){
			$this->display();
		}	
		
		//完成登录功能
		public function loginSubmit(){
			if($_SESSION['verify'] != md5($_POST['verify'])) {
				$this->error('验证码错误！');
				exit();
			}
			$user = M('User');
			$data = $user->field('id,user,password')
						->getByUser($_POST['user']);
			if($data && $data['password'] == md5($_POST['password'])){
				//设置cookie和其到期时间
				$_POST['time'] = empty($_POST['time']) ? 0 :$_POST['time'];
				cookie('name',$data['user'],$_POST['time']);
				cookie('auth',md5($data['user'].'zgcms'),$_POST['time']);
				//修改最后登录时间
				$save = array();
				$save['login_time'] = time();
				$user->where('id = '.$data['id'])->save($save);
				$this->success('登录成功！','__APP__');
			}else{
				$this->error('用户名或密码错误！');
			}
		}
		
		//退出登录
		public function logout(){
			cookie('name',null);
			cookie('auth',null);
			$this->success('退出登录成功！','__APP__');
		}
	}
?>